import Toast from "@vant/weapp/toast/toast";
Page({
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      }
      if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    show: false,
    showc: 0,
    house: '请选择房屋信息',
    now: '请选择预约日期',
    form: {
      mobile: '',
      gender: '0',
      name: '',
    },
  },
  handleGenderChange(e: any) {
    this.setData({
      'form.gender': e.detail.value
    })
  },
  async sub() {
    console.log(this.data.form)
    if (!this.data.form.name) return Toast('请输入姓名');
    if (!this.data.form.visitDate) return Toast('请选择预约日期');
    if (!(/^1[3-9]\d{9}$/.test(this.data.form.mobile))) return Toast('手机号格式错误');
    if (!this.data.form.houseId) return Toast('请选择房屋信息');
    let res = await wx.http.post('/visitor', this.data.form)
    Toast(res.message)
    if (res.code == 10000) {
      wx.navigateTo({
        url: '/pages/ewm/ewm?id=' + res.data.id
      })
    }
  },
  onInput(event) {
    this.setData({
      now: this.gettime(event.detail, '/'),
      'form.visitDate': this.gettime(event.detail, '-'),
      show: false,
    });
  },
  namefn(e) {
    this.setData({
      'form.name': e.detail.value
    })
  },
  mobilefn(e) {
    this.setData({
      'form.mobile': e.detail.value
    })
  },
  viewTap: function (e) {
    this.setData({
      'form.houseId': e.currentTarget.dataset.item.id,
      house: e.currentTarget.dataset.item.point + e.currentTarget.dataset.item.building + e.currentTarget.dataset.item.room,
      show: false
    })
  },
  datafn() {
    this.setData({
      show: true,
      showc: 3
    })
  },
  showfn() {
    this.setData({
      show: true,
      showc: 1
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  async onLoad() {
    let res = await wx.http.get('/room')
    let arr = res.data.filter(item => item.status == 2)
    this.setData({
      list: arr
    })
    let res2 = await wx.http.get('/repairItem')
    this.setData({
      list2: res2.data
    })
  },
  gettime(time, t) {
    const timestamp = time;
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const formattedDate = year + t + month + t + day;
    return formattedDate
  }
})